package it.project;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @Author: Administrator
 * @Description:
 * @Date: 2024/10/20 下午6:13
 * @Version: 1.0
 */
public class Demo1 {
   private static   Scanner input= new Scanner(System.in);
   private static   Random random = new Random();
   private static ArrayList<String> testList=new ArrayList<>();
   private static ArrayList<String> onlineList=new ArrayList<>();
   private static ArrayList<String> errList=new ArrayList<>();
   private static int succes=0;

    public static void main(String[] args) {
        showFirstMenu();


        }
    private static void  showFirstMenu(){
        System.out.println("===================欢迎进入主界面=====================");
            System.out.println( "1 测试数据\t 2 答题系统\t 3 退出 ");
            while (true){
                switch (input.nextInt()) {
                    case 1:
                        // todo 测试数据
                        test();
                        break;
                    case 2:
                        // todo 答题系统
                        showSecondMenu();
                        break;
                    case 3:
                        // 退出界面
                     return;
                    default:
                        System.out.println("输入指令有误，请重新输入");

                }
            }

    }

    private static void showSecondMenu() {
        System.out.println("=================欢迎进入答题系统==============");
        System.out.println("1 计算 2 错题界面 3 返回上级菜单");
        System.out.println("请输入你的选择：");
        while (true){
            switch (input.nextInt()) {
                case 1:
                    //todo计算 加法、减法、除法、混合
                    showThirdMenu();

                    break;
                case 2:
                    //todo查看你的错误
                  showErrorMenu();
                    break;
                case 3:
                    showFirstMenu();
                default:
                    System.out.println("你输入的指令有误，请重新输入");
            }
        }

    }

    private static void showErrorMenu() {
        System.out.println("===========================欢迎查错、改错============================");
        System.out.println("1 查看错题 \t 2 重写错题 \t 3 返回上级目录");
        while (true){
            switch (input.nextInt()){
                case 1:
                    showError();
                    break;
                case 2:
                    secondWriteError();
                    break;
                case 3:
                    showSecondMenu();
                    break;
                default:
                    System.out.println("========输入有误，请重新输入====");
            }

        }



    }



    private static void secondWriteError(){
     sendExpression(errList);

    }
    private static void showError() {
        int count=0;
        System.out.println("=====================欢迎进入查看错误界面==========================");
        if (errList.isEmpty()){
            System.out.println("==================当前没有错误====================");
        }
        else {
            System.out.println("============================================================");
            for (String s : errList) {
                System.out.printf ("%s\t\t",s);
                count++;
                if (count%6==0){
                    System.out.println();

                }
            }
            System.out.println();
            System.out.println("错题集共有："+errList.size()+"错题");
            System.out.println("============================================================");

        }
        while (true){
            System.out.println("是否要退出当前界面:YES 退出 、NO 不");
            String next = input.next();
            if(next.equalsIgnoreCase("YES")){
               showErrorMenu();
                break;

            }
            else {
                System.out.println("继续查看当前界面");
            }
        }

    }

    private static void showThirdMenu() {
        System.out.println("=====================欢迎进入计算界面=============================");
        System.out.println(" 1 加法\t 2 减法\t 3 混合习题（+/-）\t 4 错题界面\t5 返回上级菜单");
        while (true){
            switch (input.nextInt()){
                case 1:
                    //todo 加法
                    doAddition();
                    break;
                case 2:
                    //todo 减法
                    doSubtraction();
                    break;
                case 3:
                    //todo 混合习题
                    doMixed();
                    break;
                case 4:
                    // todo 错题
                    showErrorMenu();
                    break;
                case 5:
                    showSecondMenu();
                    break;
                default:
                    System.out.println("==============输入有误，请重新输入===============");
            }
        }



    }

    private static void doMixed() {
        System.out.println("==============================欢迎进入混合练习界面================================");
        inputNum("ADDITION_OR_SUBTRACTION");
    }

    static int randomNum = 50;
    private static void doSubtraction() {
        System.out.println("=====================欢迎进入减法练习界面=========================");
       inputNum("subtraction");
    }

private static void inputNum( String name){
    System.out.println("请输入，你想要练习题数：");
    exeriseNum = input.nextInt();
    System.out.println("请输入，你想要练习的难易程度：1: 1-50以内\t2: 1-100以内\t 3: 1-500以内\t4: 1-1000 ");
    int difficultOresay=input.nextInt();
    switch (difficultOresay){
        case 1:
            print(name, randomNum);
            sendExpression(onlineList);
            break;
        case 2:
            randomNum=100;
            print(name, randomNum);
            sendExpression(onlineList);
          //  a(randomNum);
            break;
        case 3:
            randomNum=500;
            print(name, randomNum);
            sendExpression(onlineList);
          //  a(randomNum);
            break;
        case 4:
            randomNum=1000;
            print(name, randomNum);
            sendExpression(onlineList);
           // a(randomNum);
            break;
    }
}
    static int exeriseNum;
    private static void doAddition() {
        System.out.println("=====================欢迎进入加法练习界面=========================");
        inputNum( "addition");
    }
private static void removeEle(){
    if (!onlineList.isEmpty()){
            //System.out.println(list1.get(i ));
         onlineList.clear();
            //list1.remove(list1.get(i ));

    }
}
private static void  print(String name,int randomNum){
        removeEle();
        String z=null;
        String x;
        String y;
     int count=0;
    for (int i = 0; i <exeriseNum ; i++) {
        x = random.nextInt(randomNum)+1+"";
        y = random.nextInt(randomNum)+1+"";
        if (name.equalsIgnoreCase(Arimethic.ADDITION.name())){

             z=x+"+"+y+"=?";
        }
        else if (name.equalsIgnoreCase(Arimethic.SUBTRACTION.name())){
            if (Integer.parseInt(x)>Integer.parseInt(y)){
                z=x+"-"+y+"=?";
            }
            else {
                z=y+"-"+x+"=?";
            }

        }
        else if (name.equalsIgnoreCase(Arimethic.ADDITION_OR_SUBTRACTION.name())){
            boolean b = random.nextBoolean();
            if (b){
                z=x+"+"+y+"=?";
            }
          else{
                if (Integer.parseInt(x)>Integer.parseInt(y)){
                    z=x+"-"+y+"=?";
                }
                else {
                    z=y+"-"+x+"=?";
                }
            }
        }

        System.out.printf("%s\t\t",z);
       onlineList.add(z);
        count++;
        if (count%6==0){
            System.out.println();
            System.out.println("======================================================================");
        }

    }
    System.out.println();
    System.out.println("======================================================================");

}
private static void sendExpression(ArrayList<String> list){
        succes=0;
    dataPractice(list);
    if (succes==list.size()){
        System.out.println("================恭喜你，全部答对了！！！！！！！！！！=================");
    }
    else {
        System.out.println("================答对："+succes+"题还需继续加油！================");
    }
    showThirdMenu();
}
    private static void test(){

       String []s = new String [20];
       s[0]="a+3=?";
       s[1]="a+b=?";
       s[2]="a+A=?";
       s[3]="true+B=?";
       s[4]="2+5=?";
       s[5]="2+6=?";
       s[6]="212+72=?";
       s[7]= " + =?";
       s[8]="12+24=?";
       s[9]="25+80=?";
       s[10]= " + =?";
       s[11]="A+24=?";
       s[12]="25+z=?";
       s[13]= " +b=?";
       s[14]="12+b=?";
       s[15]="79+80=?";
       s[16]="12-b=?";
       s[17]="86-80=?";
        s[18]="12-16=?";
        s[19]="86-37=?";

        System.out.println("=======以下是不符合条件：============");
       int count=0;

       for (int i = 0; i <s.length ; i++) {
           for (int j = 0; j <s[i].length() ; j++) {
               if (s[i ].charAt(j)>='a'&&s[i ].charAt(j)<='z'||s[i ].charAt(j)>='A'&&s[i ].charAt(j)<='Z'||s[i].charAt(j)==' ') {
                   System.out.printf("%s\t\t",s[i]);
                   count++;
                   if (count%6==0){
                       System.out.println();
                   }
                   break;

               }
               else {
                   if (s[i ].charAt(j)=='='){
                       testList.add(s[i]);
                       break;
                   }

               }
           }


       }
       System.out.println();

       //  System.out.println(list);
       System.out.println("============================");
       System.out.println("是否需要使用测试数据，计算：YES 需要 NO 不需要");
       System.out.println("请输入：");
       String yourAnswer = input.next();
       boolean flag = yourAnswer.equalsIgnoreCase("YES");
       if (flag){
         sendExpression(testList);

       }
      showFirstMenu();


    }
    private static void dataPractice(ArrayList<String> list){
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String s1 = iterator.next();
            System.out.print(s1);
            System.out.println();
            System.out.println("请输入结果：");
            int sum = getResult(s1);
            int count = 0;
            while (true) {
                int num = input.nextInt();
                if (num == sum) {
                    System.out.println("========恭喜你，你的回答正确========");
                     if (list==errList){
                         iterator.remove();
                         System.out.println("错题集：目前有"+list.size()+"错题");

                     }
                    // list.remove(s1);

                    succes++;
                    break;
                }
                System.out.println("回答错误，请重新输入：");
                count++;
                System.out.println("当前还有：" + (2 - count) + "次");
                if (count % 2 == 0) {
                    System.out.println("====================机会用完，自动跳过本题======================");
                    System.out.println("===================将本题目，保存到错题集中===================");
                    //todo 将本题目，保存到错题集中
                    if (list!=errList){
                        errList.add(s1);
                    }

                    break;
                }
            }
        }
//        for (String s1 : list) {
//         System.out.println(s1);
//         System.out.println("请输入结果：");
//         int  sum = getResult(s1);
//         int count=0;
//         while(true){
//             int  num = input.nextInt();
//             if (num==sum) {
//                 System.out.println("========恭喜你，你的回答正确========");
//                 if (list==errList){
//                     list1.add(s1);
//                 }
//                // list.remove(s1);
//
//                 succes++;
//                 break;
//             }
//             System.out.println("回答错误，请重新输入：");
//             count++;
//             System.out.println("当前还有："+(2-count)+"次");
//             if (count%2==0){
//                 System.out.println("====================机会用完，自动跳过本题======================");
//                 System.out.println("===================将本题目，保存到错题集中===================");
//                 //todo 将本题目，保存到错题集中
//                 errList.add(s1);
//                 break;
//             }
//         }
//
//
//     }
//        errList.removeAll(list1);

 }
    private static int getResult(String s){
        int sum=0;
        for (int i = 0; i <s.length() ; i++) {
            if (s.charAt(i)=='+'){
                Pattern pattern = Pattern.compile("\\d+");
                // 创建Matcher对象
                Matcher matcher = pattern.matcher(s);
                // 查找并打印所有匹配的数字
                while (matcher.find()) {
                    sum+=Integer.parseInt(matcher.group());
                }
            }

            else if (s.charAt(i)=='-'){
               String[] sj = s.split("=");
               String[] in = sj[0].split("-");
               int num1=Integer.parseInt(in[0]);
               int num2=Integer.parseInt(in[1]);
               sum=num1-num2;
            }
        }

        return sum;

    }
}


